﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.IO;
using System.Text;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Reflection;

using ECCIT;
using ECCIT.Configuration;
using ECCIT.Utilities;

namespace ECCIT.Web
{
    public partial class XmlHttpGate : System.Web.UI.Page
    {
        private void Page_Load(object sender, System.EventArgs e)
        {
            // Set the response content type and content encoding
            Response.ContentType = "text/xml";
            //Response.Charset = ""
            Response.ContentEncoding = Encoding.UTF8;

            // Validate the arguments in Request.QueryString
            ResultInfo validateResult = ValidateArguments(Request);
            if (!validateResult.Result)
            {
                Response.Write(validateResult.Message);
                return;
            }

            string typeName = Request.QueryString["type"];
            string methodName = Request.QueryString["method"];
            string argString = Request.QueryString["args"]; // args format should be: int:arg1|decimal:arg2|string:arg3

            try
            {
                object[] args = null;
                if (argString != null)
                {
                    JoinedString arg = new JoinedString(argString, "|");
                    args = new object[arg.Count];
                    int i = 0;
                    foreach (string a in arg)
                    {
                        JoinedString pair = new JoinedString(a, ":");
                        switch (pair[0].ToLower())
                        {
                            case "int":
                                args[i] = int.Parse(pair[1]);
                                break;
                            case "decimal":
                                args[i] = decimal.Parse(pair[1]);
                                break;
                            case "bool":
                                args[i] = bool.Parse(pair[1]);
                                break;
                            default:
                                args[i] = pair[1];
                                break;
                        }
                        i++;
                    }
                }

                string assemblyPath = ConfigCacheManager.AssemblyTypeMappingDictionary[typeName];

                ResultInfo result = ReflectorUtil.InvokeMethod(assemblyPath, typeName, methodName, args);
                string output = string.Empty;
                if (result.Result)
                {
                    if (result.ReturnObject is string || result.ReturnObject.GetType().IsValueType)
                        output = result.ReturnObject.ToString();
                    else
                        output = JsonUtil.Serialize(result.ReturnObject);
                }
                else 
                {
                    output = result.Message;
                }

                Response.Write(output);
            }
            catch (Exception ex)
            {
                Response.Write("Error occurred when dynamically invoke method: " + methodName + " of Type: " + typeName);
            }
        }

        private ResultInfo ValidateArguments(HttpRequest request)
        {
            // type and method are required to be included in request.QueryString
            string type = request.QueryString["type"];
            if (type == null)
                return ResultInfo.CreateInstance(false, "QueryString:type not found.");

            string method = request.QueryString["method"];
            if (method == null)
                return ResultInfo.CreateInstance(false, "QueryString:method not found.");

            return ResultInfo.True;
        }
    }
}
